package com.marvels.approve.credit.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.marvels.approve.common.service.IUploadFileService;
import com.marvels.approve.common.service.McamLoanApplyInfoCommonService;
import com.marvels.approve.common.utils.SerialNumCreater;
import com.marvels.approve.credit.consum.ConsumerExternalIntBankApi;
import com.marvels.approve.credit.dto.IntBankBanruDetailDTO;
import com.marvels.approve.credit.dto.IntBankBasiInfoCollectionDTO;
import com.marvels.approve.credit.dto.IntBankBasicDTO;
import com.marvels.approve.credit.dto.IntBankCreditCardDTO;
import com.marvels.approve.credit.dto.IntBankCreditPaymentRecordsDTO;
import com.marvels.approve.credit.dto.IntBankCredittipsDTO;
import com.marvels.approve.credit.dto.IntBankDwellmessageDTO;
import com.marvels.approve.credit.dto.IntBankEndowmentInsuranceDTO;
import com.marvels.approve.credit.dto.IntBankGuaranteeCountDTO;
import com.marvels.approve.credit.dto.IntBankNotCredittipsDTO;
import com.marvels.approve.credit.dto.IntBankNotPinshouseholdsDTO;
import com.marvels.approve.credit.dto.IntBankNotQuasiCreditDTO;
import com.marvels.approve.credit.dto.IntBankPositionmesageDTO;
import com.marvels.approve.credit.dto.IntBankQueryDetailDTO;
import com.marvels.approve.credit.dto.IntBankQuerySummaryDTO;
import com.marvels.approve.credit.dto.IntBankStandardCreditCardDTO;
import com.marvels.approve.credit.dto.IntBankTotalDTO;
import com.marvels.approve.credit.dto.IntDataQueryLogDTO;
import com.marvels.approve.credit.service.IntBankService;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.manager.dto.ActivitiNodeDTO;
import com.marvels.approve.process.dao.ILoanApplyCaseDao;
import com.marvels.approve.process.dto.CompleteProcessDTO;
import com.marvels.approve.process.dto.LaAttachAtmeTypeViewDTO;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.ProcessApproveDTO;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.approve.process.service.TaskProcessService;

import com.marvels.common.constants.CaseNodeConstant;
import com.marvels.common.constants.LoanStateConstant;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;

@Service
public class IntBankServiceImpl implements IntBankService {
	
	@Autowired
	private ConsumerExternalIntBankApi consumerExternaIntBanklApi;

	@Autowired
	private ILoanApplyCaseDao loanApplyCaseMapper;
	
	@Autowired
	private TaskProcessService taskProcessService;
	
	@Autowired
	private IMcamLoanApplyInfoDao applyInfoDao;
	
	@Autowired
	private ILoanApplyCaseDao caseDao;
	
	@Autowired
	private IProcessService processService;
	
	@Autowired
	private IUploadFileService uploadFileService;
	
	
	@Autowired
	private McamLoanApplyInfoCommonService mcamLoanApplyInfoCommonService;
	/**
	 * 初始化保单数据
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public IntBankBasiInfoCollectionDTO checktheCreditReport(LoanApplyCaseDTO param) throws MarvelsBusinessException {
//		param.setLoanId("18062815365790505");
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("loanId", param.getLoanId());
		IntBankBasiInfoCollectionDTO intBankBasiInfoCollection = null;
		try{
			// 获取缓存的数据
			intBankBasiInfoCollection = consumerExternaIntBanklApi.getMarvelsCreditReportData(dataMap);
			if (intBankBasiInfoCollection == null) {
				throw new MarvelsBusinessException("个人信用报告为空");
			}
		}catch(Exception e){
			throw new MarvelsBusinessException("调用dubbo获取个人信用报告异常",e);
		}
		
		// 用户影像资料附件
		List<LaAttachAtmeTypeViewDTO> mediaList = uploadFileService.getAttachmentsByViewKey(CaseNodeConstant.WOCREDITCHECK.getCode(), param.getLoanId());

		intBankBasiInfoCollection.setMediaList(mediaList);
		return intBankBasiInfoCollection;
	}

	/**
	 * 添加 或 修改 个人基本信息
	 * 
	 * @return
	 */
	public IntBankBasicDTO saveIntBankBasicList(IntBankBasicDTO intBankBasic)  throws MarvelsBusinessException{
		if(intBankBasic == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankBasicDTO data = null;
		if (intBankBasic.getId() == null || intBankBasic.getId() < 1) {
			intBankBasic.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankBasic(intBankBasic);
		} else {
			intBankBasic.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankBasic(intBankBasic);
		}
		return data;
	}

	/**
	 * 添加 或 修改居住信息
	 * 
	 * @param param
	 * @return
	 * @throws MarvelsBusinessException
	 */
	public IntBankDwellmessageDTO saveIntBankDwellmessage(IntBankDwellmessageDTO intBankDwellmessage)  throws MarvelsBusinessException{
		if(intBankDwellmessage == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankDwellmessageDTO data =consumerExternaIntBanklApi.saveMarvelsIntBankDwellmessage(intBankDwellmessage);
		return data;
	}
	
	/**
	 * 添加 或 修改职业信息
	 */
	public IntBankPositionmesageDTO addProfession(IntBankPositionmesageDTO intBankPositionmesage)  throws MarvelsBusinessException{
		if(intBankPositionmesage == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankPositionmesageDTO data =consumerExternaIntBanklApi.saveMarvelsIntBankPositionmesage(intBankPositionmesage);
		return data;
	}
	
	/**
	 * 信用提示
	 * 
	 * @return
	 */
	public IntBankCredittipsDTO updateCredittips(IntBankCredittipsDTO intBankCredittips)  throws MarvelsBusinessException{
		if(intBankCredittips == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankCredittipsDTO data = null;
		if (intBankCredittips.getId() == null || intBankCredittips.getId() < 1) {
			intBankCredittips.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankCredittips(intBankCredittips);
		} else {
			intBankCredittips.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankCredittips(intBankCredittips);
		}
		return data;
	}

	/**
	 * 未结清贷款信息汇总 新添 或 修改
	 * 
	 * @return
	 */
	public IntBankNotCredittipsDTO updateIntBankNotCredittips(IntBankNotCredittipsDTO intBankNotCredittips)  throws MarvelsBusinessException{
		if(intBankNotCredittips == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankNotCredittipsDTO data = null;
		if (intBankNotCredittips.getId() == null || intBankNotCredittips.getId() < 1) {
			intBankNotCredittips.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankNotCredittips(intBankNotCredittips);
		} else {
			intBankNotCredittips.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankNotCredittips(intBankNotCredittips);
		}
		return data;
	}

	/**
	 * 未销户贷记卡信息汇总 新添 或 修改
	 * 
	 * @return
	 */
	public IntBankNotPinshouseholdsDTO updateIntBankNotPinshouseholds(IntBankNotPinshouseholdsDTO intBankNotPinshouseholds)  throws MarvelsBusinessException{
		if(intBankNotPinshouseholds == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankNotPinshouseholdsDTO data = null;
		if (intBankNotPinshouseholds.getId() == null || intBankNotPinshouseholds.getId() < 1) {
			intBankNotPinshouseholds.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankNotPinshouseholds(intBankNotPinshouseholds);
		} else {
			intBankNotPinshouseholds.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankNotPinshouseholds(intBankNotPinshouseholds);
		}
		return data;
	}

	/**
	 * 添加准贷记卡信息详细
	 * 
	 * @return
	 */
	public Integer addStandardCredit(IntBankStandardCreditCardDTO intBankStandardCreditCard)  throws MarvelsBusinessException{
//		intBankStandardCreditCard.setLoanId(intBankStandardCreditCard.getLoanId());
		intBankStandardCreditCard.setSerialNum(intBankStandardCreditCard.getLoanId());
		intBankStandardCreditCard.setEstablishDate(new Date());
		Integer count = consumerExternaIntBanklApi.saveIntBankStandardCreditCard(intBankStandardCreditCard);
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankStandardCreditCard.getLoanId());
		//List<IntBankStandardCreditCardDTO> intBankStandardCreditCardDTO = consumerExternaIntBanklApi.intBankStandardCreditCard(map);
		//if (intBankStandardCreditCardDTO != null && intBankStandardCreditCardDTO.size() > 0) {
			// this.setStandardCreditCardList(intBankStandardCreditCard);
		//}
		return count;
	}

	/**
	 * 修改 准贷记卡信息详细
	 * 
	 * @return
	 */
	public Integer updateStandardCredit(IntBankStandardCreditCardDTO intBankStandardCreditCard)  throws MarvelsBusinessException{
		intBankStandardCreditCard.setAmendDate(new Date());
		Integer  count = consumerExternaIntBanklApi.updateIntBankStandardCreditCard(intBankStandardCreditCard);
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankStandardCreditCard.getSerialNum());
//		List<IntBankStandardCreditCardDTO> intBankStandardCreditCardDTO = consumerExternaIntBanklApi
//				.intBankStandardCreditCard(map);
//		if (intBankStandardCreditCardDTO != null && intBankStandardCreditCardDTO.size() > 0) {
//			// this.setStandardCreditCardList(intBankStandardCreditCard);
//		}
		return count;
	}

	/**
	 * 修改 或 添加 养老保险金数据
	 * 
	 * @return
	 */
	public Integer updateDetail(IntBankEndowmentInsuranceDTO intBankEndowmentInsurance)  throws MarvelsBusinessException{
		Integer count;
		if (intBankEndowmentInsurance.getId() == null || intBankEndowmentInsurance.getId()==0l) {
			intBankEndowmentInsurance.setEstablishDate(new Date());
			count = consumerExternaIntBanklApi.saveIntBankEndowmentInsurance(intBankEndowmentInsurance);
		} else {
			intBankEndowmentInsurance.setAmendDate(new Date());
			count = consumerExternaIntBanklApi.updateIntBankEndowmentInsurance(intBankEndowmentInsurance);
		}
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankEndowmentInsurance.getLoanId());
//		List<IntBankEndowmentInsuranceDTO> intBankEndowmentInsuranceDTO = consumerExternaIntBanklApi.intBankEndowmentInsurance(map);
//		if (intBankEndowmentInsuranceDTO != null && intBankEndowmentInsuranceDTO.size() > 0) {
//			// this.setIntBankEndowmentInsurance(intBankEndowmentInsuranceDTO.get(0));
//		}
		return count;
	}

	/**
	 * 未销户准贷记卡信息汇总 新添 或 修改
	 * 
	 * @return
	 */
	public IntBankNotQuasiCreditDTO updateQuasiCredit(IntBankNotQuasiCreditDTO intBankNotQuasiCredit)  throws MarvelsBusinessException {
		if(intBankNotQuasiCredit == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		IntBankNotQuasiCreditDTO data = null;
		if (intBankNotQuasiCredit.getId() == null || intBankNotQuasiCredit.getId() < 1) {
			intBankNotQuasiCredit.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankNotQuasiCredit(intBankNotQuasiCredit);
		} else {
			intBankNotQuasiCredit.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankNotQuasiCredit(intBankNotQuasiCredit);
		}
		return data;
	}

	/**
	 * 未结清贷款信息汇总 新添 或 修改
	 * 
	 * @return
	 */
	public IntBankGuaranteeCountDTO updateGuaranteeCount(IntBankGuaranteeCountDTO intBankGuaranteeCount)  throws MarvelsBusinessException {
		IntBankGuaranteeCountDTO data = null;
		intBankGuaranteeCount.setCreatedBy(CacheUtils.getCacheUser().getUserCode());
		intBankGuaranteeCount.setCreatedDate(new Date());
		intBankGuaranteeCount.setUpdatedBy(CacheUtils.getCacheUser().getUserCode());
		intBankGuaranteeCount.setUpdatedDate(new Date());
		if (intBankGuaranteeCount.getId() == null || intBankGuaranteeCount.getId()==0l) {
			 data = consumerExternaIntBanklApi.saveMarvelsIntBankGuaranteeCount(intBankGuaranteeCount);
		} else {
			 consumerExternaIntBanklApi.updateIntBankGuaranteeCount(intBankGuaranteeCount);
		}
		return data;
	}

	/**
	 * 添加贷款信息
	 * 
	 * @return
	 */
	public Integer updateLoanRecord(IntBankCreditPaymentRecordsDTO intBankCreditPaymentRecords)  throws MarvelsBusinessException{
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankCreditPaymentRecords.getLoanId());
		
//		Map<String, Object> dataMap = consumerExternaIntBanklApi.intBankCreditPaymentRecords(map);
//		Object intBankCreditPaymentNJQRecords = dataMap.get("intBankCreditPaymentNJQRecords");
//		List<IntBankCreditPaymentRecordsDTO> intBankCreditPaymentNJQ = null;
//		if (intBankCreditPaymentNJQRecords != null) {
//			intBankCreditPaymentNJQ = (List<IntBankCreditPaymentRecordsDTO>) intBankCreditPaymentNJQRecords;
			// this.setPaymentRecordsList(intBankCreditPaymentNJQ);
//		}

//		Object intBankCreditPaymentJQRecords = dataMap.get("intBankCreditPaymentJQRecords");
//		List<IntBankCreditPaymentRecordsDTO> intBankCreditPaymentJQ = null;
//		if (intBankCreditPaymentJQ != null) {
//			intBankCreditPaymentJQ = (List<IntBankCreditPaymentRecordsDTO>) intBankCreditPaymentNJQRecords;
			// this.setSettlePaymentRecordsList(intBankCreditPaymentJQ);
//		}

//		return consumerExternaIntBanklApi.saveIntBankCreditPaymentRecords(intBankCreditPaymentRecords);
		return null;
	}

	/**
	 * 修改贷款信息
	 * 
	 * @return
	 */
	public Integer updateRecord(IntBankCreditPaymentRecordsDTO intBankCreditPaymentRecords) throws MarvelsBusinessException{
		intBankCreditPaymentRecords.setAmendDate(new Date());
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankCreditPaymentRecords.getLoanId());
//		Map<String, Object> dataMap = consumerExternaIntBanklApi.intBankCreditPaymentRecords(map);
//		Object intBankCreditPaymentNJQRecords = dataMap.get("intBankCreditPaymentNJQRecords");
//		List<IntBankCreditPaymentRecordsDTO> intBankCreditPaymentNJQ = null;
//		if (intBankCreditPaymentNJQRecords != null) {
//			intBankCreditPaymentNJQ = (List<IntBankCreditPaymentRecordsDTO>) intBankCreditPaymentNJQRecords;
//			// this.setPaymentRecordsList(intBankCreditPaymentNJQ);
//		}
//
//		Object intBankCreditPaymentJQRecords = dataMap.get("intBankCreditPaymentJQRecords");
//		List<IntBankCreditPaymentRecordsDTO> intBankCreditPaymentJQ = null;
//		if (intBankCreditPaymentJQ != null) {
//			intBankCreditPaymentJQ = (List<IntBankCreditPaymentRecordsDTO>) intBankCreditPaymentNJQRecords;
//			// this.setSettlePaymentRecordsList(intBankCreditPaymentJQ);
//		}
		return consumerExternaIntBanklApi.updateIntBankCreditPaymentRecords(intBankCreditPaymentRecords);

	}

	

	/**
	 * 修改贷记卡信息
	 * 
	 * @return
	 */
	 @Transactional(timeout = 30 ,rollbackFor = Exception.class)
	public Integer updateCredit(IntBankCreditCardDTO intBankCreditCard)  throws MarvelsBusinessException{
		intBankCreditCard.setAmendDate(new Date());
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankCreditCard.getLoanId());
//		Map<String, Object> dataMap = consumerExternaIntBanklApi.intBankCreditPaymentRecords(map);
//		Object intBankCreditCardNXh = dataMap.get("intBankCreditCardNXh");
//		List<IntBankCreditCardDTO> intBankCreditCardNXhRecord = null;
//		if (intBankCreditCardNXh != null) {
//			intBankCreditCardNXhRecord = (List<IntBankCreditCardDTO>) intBankCreditCardNXh;
//			// this.setIntBankCreditCardList(intBankCreditCardNXhRecord);
//		}
//
//		Object intBankCreditCardXh = dataMap.get("intBankCreditCardXh");
//		List<IntBankCreditCardDTO> intBankCreditCardXhRecord = null;
//		if (intBankCreditCardXh != null) {
//			intBankCreditCardXhRecord = (List<IntBankCreditCardDTO>) intBankCreditCardXh;
//			// this.setCreditCardList(intBankCreditCardXhRecord);
//		}
		return consumerExternaIntBanklApi.updateIntBankCreditCard(intBankCreditCard);

	}

	// 添加记录
	public Integer addQueryDetail(IntBankQueryDetailDTO intBankQueryDetail)  throws MarvelsBusinessException{
		if (intBankQueryDetail == null) {
			intBankQueryDetail = new IntBankQueryDetailDTO();
		}
		Integer count = 0;
		if (intBankQueryDetail.getQueryTheDate() != null && StringUtils.isNotEmpty(intBankQueryDetail.getQueryOperators())) {
			intBankQueryDetail.setEstablishDate(new Date());
			count = consumerExternaIntBanklApi.saveIntBankQueryDetail(intBankQueryDetail);
		}

//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankQueryDetail.getLoanId());
//		Map<String, Object> dataMap = consumerExternaIntBanklApi.addQueryDetailPositionData(map);
//		Object intBankQuerySummaryObj = dataMap.get("intBankQuerySummary");
//		List<IntBankQuerySummaryDTO> intBankQuerySummary = (List<IntBankQuerySummaryDTO>) intBankQuerySummaryObj;
//		if (intBankQuerySummary != null && intBankQuerySummary.size() > 0) {
//			// this.setIntBankQuerySummary(intBankQuerySummary.get(0));
//		}
//
//		Object intBankQueryDetailObj = dataMap.get("intBankQueryDetail");
//		List<IntBankQueryDetailDTO> intBankQueryDetailDTO = null;
//		if (intBankQueryDetailObj != null) {
//			intBankQueryDetailDTO = (List<IntBankQueryDetailDTO>) intBankQueryDetailObj;
//			// this.setIntBankQueryDetailList(intBankQueryDetail);
//		}

		return count;
	}

	// 添加个人记录
	public Integer addBanruDetail(IntBankBanruDetailDTO intBankBanruDetail) throws MarvelsBusinessException {
		 intBankBanruDetail.setCreatedBy(getUserCode());
		 intBankBanruDetail.setCreatedDate(new Date());
		 intBankBanruDetail.setUpdatedDate(new Date());
		 Integer count = 0;
		if (StringUtils.isNotEmpty(intBankBanruDetail.getQueryOperators())
				&& StringUtils.isNotEmpty(intBankBanruDetail.getQueryTheDate())) {
			count = consumerExternaIntBanklApi.saveIntBankBanruDetail(intBankBanruDetail);
		}
//		Map<String, String> map = new HashMap<>();
//		map.put("serialNum", intBankBanruDetail.getLoanId());
//		List<IntBankBanruDetailDTO> intBankBanruDetailDTO = consumerExternaIntBanklApi.intBankBanruDetail(map);
//		if (intBankBanruDetailDTO != null && intBankBanruDetailDTO.size() > 0) {
			// this.setIntBankBanruDetailList(intBankBanruDetail);
//		}
		/* getCreditReport(intBankQueryDetail.getSerialNum()); */
		return count;
	}

	public Integer deleteQueryDetail(IntBankQueryDetailDTO intBankQueryDetail)  throws MarvelsBusinessException{
		if(intBankQueryDetail==null || intBankQueryDetail.getId()==null || intBankQueryDetail.getId()<1){
			throw new MarvelsBusinessException("入参为空或id为空");
		}
		Integer count= consumerExternaIntBanklApi.deleteIntBankQueryDetail(intBankQueryDetail.getId());
		return count;

	}

	public Integer deleteBanruDetail(IntBankBanruDetailDTO intBankBanruDetail)  throws MarvelsBusinessException{
		if(intBankBanruDetail==null || intBankBanruDetail.getId()==null || intBankBanruDetail.getId()<1){
			throw new MarvelsBusinessException("入参为空或id为空");
		}
		Integer count = consumerExternaIntBanklApi.deleteIntBankBanruDetail(intBankBanruDetail.getId());
		return count;
	}

	// 修改查询汇总信息
	public Integer updateQueryLog(IntBankQuerySummaryDTO intBankQuerySummary)  throws MarvelsBusinessException{
		Map<String, String> dataMap = new HashMap<String, String>();
//		String serialNum = "";
		Integer count = 0;
		if (consumerExternaIntBanklApi.intBankQuerySummary(dataMap).size() == 0) {
			intBankQuerySummary.setEstablishDate(new Date());
			count = consumerExternaIntBanklApi.saveIntBankQuerySummary(intBankQuerySummary);
		} else {

			Map<String, String> map = new HashMap<>();
			map.put("serialNum", intBankQuerySummary.getLoanId());
			List<IntBankQuerySummaryDTO> intBankPositionmesage = consumerExternaIntBanklApi.intBankQuerySummary(map);
			IntBankQuerySummaryDTO intBank = new IntBankQuerySummaryDTO();
			List<IntBankQuerySummaryDTO> intBankPositionList = (List<IntBankQuerySummaryDTO>) intBankPositionmesage;
			if (!CollectionUtils.isEmpty(intBankPositionList)) {
				intBank = intBankPositionList.get(0);
			}
			intBank.setAmendDate(new Date());
			intBank.setInstitutionCreditCount(intBankQuerySummary.getInstitutionCreditCount());
			intBank.setInstitutionLoanCount(intBankQuerySummary.getInstitutionLoanCount());
			intBank.setCreditCount(intBankQuerySummary.getCreditCount());
			intBank.setLoanCount(intBankQuerySummary.getLoanCount());
			intBank.setBanruCount(intBankQuerySummary.getBanruCount());
			intBank.setPostLoanCount(intBankQuerySummary.getPostLoanCount());
			intBank.setAssuranceCount(intBankQuerySummary.getAssuranceCount());
			intBank.setContributingCount(intBankQuerySummary.getContributingCount());
			count = consumerExternaIntBanklApi.updateIntBankQuerySummary(intBank);
		}

		return count;
	}

	// 录入信息完后提交，将订单 状态改变，并记录一条流水，将所有表中有插入数据的serial_num改变成所要的 提交
	@SuppressWarnings("unchecked")
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	public Integer submitState(String loanId, String card) throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info(">>>>>>>>submitState录入信息提交loanId:" + loanId);
		McamLoanApplyInfoDTO acLoanApplyInfo = taskProcessService.getLoanInfo(loanId);// 申请书编号
		Map<String, String> intBankBasicMap = new HashMap<>();
		intBankBasicMap.put("loanId", loanId);
		IntBankBasicDTO intBankBasic = null;
		List<IntBankBasicDTO> intBankBasicList = consumerExternaIntBanklApi.intBankBasic(intBankBasicMap);
		if (intBankBasicList != null && intBankBasicList.size() > 0) {
			intBankBasic = intBankBasicList.get(0);
		}
		if (acLoanApplyInfo == null || intBankBasic == null) {
			throw new MarvelsBusinessException("此人的征信报告有误，请核对是否为本人！");
		} else if (acLoanApplyInfo != null && intBankBasic != null) {
			if (!acLoanApplyInfo.getCustCardNo().equals(intBankBasic.getApplicantIdNbr())) {
				throw new MarvelsBusinessException("此人的征信报告有误，请核对是否为本人！");
			}
		}

		// 往第三方日志表中插入一条记录 以供征信页面使用
		String serialNum = SerialNumCreater.creater("HQ", loanId);
		Timestamp date = new Timestamp(System.currentTimeMillis());
		MarvelsLogUtil.getInstance().info("-----------submitState--------------1111-----" + loanId);
		IntDataQueryLogDTO intDataQueryLog = new IntDataQueryLogDTO();
		intDataQueryLog.setLoanId(loanId);
		intDataQueryLog.setSerialNum(serialNum);
		intDataQueryLog.setRequestTime(date);
		intDataQueryLog.setResponseTime(date);
		intDataQueryLog.setResponseCode("100000");
		intDataQueryLog.setChannel(MarvelsConstant.DITCH_CHANNEL);// 什么东西？
		consumerExternaIntBanklApi.insertDataLog(intDataQueryLog);

		// 是保单贷的话先判断有没有截屏
		if ("".equals(acLoanApplyInfo.getProductType())) {

		}

		LoanApplyCaseDTO caseInfo = caseDao.getEntityByLoanId(loanId);
		CompleteProcessDTO proDto = new CompleteProcessDTO();

		ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
		// 退回到app
		processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);

		proDto.setProcessApproveDto(processApproveDto);
		proDto.setLoanId(loanId);

		ActivitiNodeDTO curNode = new ActivitiNodeDTO();
		curNode.setNodeCode(caseInfo.getCurNodeCode());
		proDto.setCurNode(curNode);
		LoanApplyCaseDTO caseDTO = processService.completeLoanApplyCase(proDto);
		// 更新工单跟踪表
		this.caseDao.updateByPrimaryKeySelective(caseDTO);

		// 更新进件表状态
//		McamLoanApplyInfoDTO infoDTO = new McamLoanApplyInfoDTO();
//		infoDTO.setStatus(LoanStateConstant.Z01);
//		infoDTO.setLoanId(loanId);
//		this.applyInfoDao.updateByLoanId(infoDTO);
		mcamLoanApplyInfoCommonService.updateMcamLoanApplyInfoStatusByLoanId(loanId,LoanStateConstant.Z01,null,null);

		//更新征信数据表serialNum
		updateIntBankData(serialNum,loanId);
		/* try {
			 //在修改之前调用一下第三方的数据获取 云保单 云房贷
			 if("4".equals(Nstatus)){
			 Map<String,Object> map = new HashMap<String,Object>();
			 map.put("loanId",loan);
			 map.put("status","0");
			 queryCaseMapper.updateBySrcCaseId(map);
			 MarvelsLogUtil.getInstance().info(loan+"===修改apply_状态submitState："+Nstatus);
			 loanInfoIDragonService.updateLoanStatus(loan, "Z01");//修改从站征信退回单的状态
			 workLogService.insertWorkLog(loan);//记录操作员日常操作
			 }else{
				 taskProcessMapper.updateLoanStatus(loan, "J00");//改变进件单 的状态
				 workLogService.insertWorkLog(loan);//记录操作员日常操作
			 }
			 } catch (Exception e) {
		 e.printStackTrace();
		 }*/
		 
		
		return null;
	}

	
	private void updateIntBankData(String serialNum,String loanId){
		Map<String, String> map = new HashMap<>();
		map.put("serialNum", loanId);
//		Map<String, Object> dataMap = consumerExternaIntBanklApi.submitStatePositionData(map);
		Map<String, Object> dataMap = null;
		List<IntBankBasicDTO> intBankBasicObj = (List<IntBankBasicDTO>) dataMap.get("intBankBasic");
		List<IntBankBasicDTO> basic = null;
		if (intBankBasicObj != null) {
			basic = intBankBasicObj;
		}
		if (basic != null && basic.size() != 0) {
			for (IntBankBasicDTO bankBasic : basic) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankBasic(bankBasic);
			}
		}
		List<IntBankCredittipsDTO> intBankCredittipsObj = (List<IntBankCredittipsDTO>) dataMap.get("intBankCredittips");
		List<IntBankCredittipsDTO> credittips = null;
		if (intBankCredittipsObj != null) {
			credittips = intBankCredittipsObj;
		}
		if (credittips != null && credittips.size() != 0) {
			for (IntBankCredittipsDTO bankBasic : credittips) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankCredittips(bankBasic);
			}
		}
		Object intBankCreditCardObj = dataMap.get("intBankCreditCard");
		List<IntBankCreditCardDTO> creditCard = null;
		if (intBankCreditCardObj != null) {
			creditCard = (List<IntBankCreditCardDTO>) intBankCreditCardObj;
		}
		if (creditCard != null && creditCard.size() != 0) {
			for (IntBankCreditCardDTO bankBasic : creditCard) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankCreditCard(bankBasic);
			}
		}
		List<IntBankCreditPaymentRecordsDTO> intBankCreditPaymentRecordsObj = (List<IntBankCreditPaymentRecordsDTO>)dataMap.get("intBankCreditPaymentRecords");
		List<IntBankCreditPaymentRecordsDTO> creditPaymentRecords =null;
		if(intBankCreditPaymentRecordsObj!=null){
			creditPaymentRecords = intBankCreditPaymentRecordsObj;
		}
		if (creditPaymentRecords != null && creditPaymentRecords.size() != 0) {
			for (IntBankCreditPaymentRecordsDTO bankBasic : creditPaymentRecords) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankCreditPaymentRecords(bankBasic);
			}
		}
		
		List<IntBankDwellmessageDTO> intBankDwellmessageObj = (List<IntBankDwellmessageDTO>) dataMap.get("intBankDwellmessage");
		List<IntBankDwellmessageDTO> dwellmessage = null;
		if (intBankDwellmessageObj != null) {
			dwellmessage = (List<IntBankDwellmessageDTO>) intBankDwellmessageObj;
		}
		if (dwellmessage != null && dwellmessage.size() != 0) {
			for (IntBankDwellmessageDTO bankBasic : dwellmessage) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankDwellmessage(bankBasic);
			}
		}
		 List<IntBankEndowmentInsuranceDTO>  intBankEndowmentInsuranceObj = (List<IntBankEndowmentInsuranceDTO>) dataMap.get("intBankEndowmentInsurance");
		List<IntBankEndowmentInsuranceDTO> endowment = null;
		if (intBankEndowmentInsuranceObj != null) {
			endowment =intBankEndowmentInsuranceObj;
		}
		if (endowment != null && endowment.size() != 0) {
			for (IntBankEndowmentInsuranceDTO bankBasic : endowment) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankEndowmentInsurance(bankBasic);
			}
		}

		List<IntBankNotCredittipsDTO> intBankNotCredittipsObj = (List<IntBankNotCredittipsDTO>) dataMap.get("intBankNotCredittips");
		List<IntBankNotCredittipsDTO> notCredittips = null;
		if (intBankNotCredittipsObj != null) {
			notCredittips = intBankNotCredittipsObj;
		}
		if (notCredittips != null && notCredittips.size() != 0) {
			for (IntBankNotCredittipsDTO bankBasic : notCredittips) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankNotCredittips(bankBasic);
			}
		}

		Object intBankNotPinshouseholdsObj = dataMap.get("intBankNotPinshouseholds");
		List<IntBankNotPinshouseholdsDTO> notPinshouseholds = null;
		if (intBankNotPinshouseholdsObj != null) {
			notPinshouseholds = (List<IntBankNotPinshouseholdsDTO>) intBankNotPinshouseholdsObj;
		}
		if (notPinshouseholds != null && notPinshouseholds.size() != 0) {
			List<IntBankNotPinshouseholdsDTO> licenses = new Gson().fromJson(new Gson().toJson(notPinshouseholds), new TypeToken<List<IntBankNotPinshouseholdsDTO>>(){}.getType());
			for (IntBankNotPinshouseholdsDTO bankBasic : licenses) {
				consumerExternaIntBanklApi.updateIntBankNotPinshouseholds(bankBasic);
			}
		}
		List<IntBankNotQuasiCreditDTO> intBankNotQuasiCreditObj = (List<IntBankNotQuasiCreditDTO>) dataMap.get("intBankNotQuasiCredit");
		List<IntBankNotQuasiCreditDTO> notQuasiCredits = null;
		if (intBankNotQuasiCreditObj != null) {
			notQuasiCredits =  intBankNotQuasiCreditObj;
		}
		if (notQuasiCredits != null && notQuasiCredits.size() != 0) {
			for (IntBankNotQuasiCreditDTO bankBasic : notQuasiCredits) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankNotQuasiCredit(bankBasic);
			}
		}

		List<IntBankPositionmesageDTO> intBankPositionmesageObj = (List<IntBankPositionmesageDTO>) dataMap.get("intBankPositionmesage");
		List<IntBankPositionmesageDTO> positionmesages = null;
		if (intBankPositionmesageObj != null) {
			positionmesages =  intBankPositionmesageObj;
		}
		if (positionmesages != null && positionmesages.size() != 0) {
			List<IntBankPositionmesageDTO> licenses = new Gson().fromJson(new Gson().toJson(positionmesages), new TypeToken<List<IntBankPositionmesageDTO>>(){}.getType());
			for (IntBankPositionmesageDTO bankBasic : licenses) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankPositionmesage(bankBasic);
			}
		}

		Object intBankQueryDetailObj = dataMap.get("intBankQueryDetail");
		List<IntBankQueryDetailDTO> queryDetails = null;
		if (intBankQueryDetailObj != null) {
			queryDetails = (List<IntBankQueryDetailDTO>) intBankQueryDetailObj;
		}
		if (queryDetails != null && queryDetails.size() != 0) {
			for (IntBankQueryDetailDTO bankBasic : queryDetails) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankQueryDetail(bankBasic);
			}
		}

		Object intBankQuerySummaryObj = dataMap.get("intBankQuerySummary");
		List<IntBankQuerySummaryDTO> querySummaries = null;
		if (intBankQuerySummaryObj != null) {
			querySummaries = (List<IntBankQuerySummaryDTO>) intBankQuerySummaryObj;
		}
		if (querySummaries != null && querySummaries.size() != 0) {
			for (IntBankQuerySummaryDTO bankBasic : querySummaries) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankQuerySummary(bankBasic);
			}
		}

		Object intBankBanruDetailObj = dataMap.get("intBankBanruDetail");
		List<IntBankBanruDetailDTO> banruDetail = null;
		if (intBankBanruDetailObj != null) {
			banruDetail = (List<IntBankBanruDetailDTO>) intBankBanruDetailObj;
		}
		if (banruDetail != null && banruDetail.size() != 0) {
			for (IntBankBanruDetailDTO bankBasic : banruDetail) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankBanruDetail(bankBasic);
			}
		}

		Object intBankStandardCreditCardObj = dataMap.get("intBankStandardCreditCard");
		List<IntBankStandardCreditCardDTO> standardCreditCards = null;
		if (intBankStandardCreditCardObj != null) {
			standardCreditCards = (List<IntBankStandardCreditCardDTO>) intBankStandardCreditCardObj;
		}
		if (standardCreditCards != null && standardCreditCards.size() != 0) {
			for (IntBankStandardCreditCardDTO bankBasic : standardCreditCards) {
				bankBasic.setSerialNum(serialNum);
				consumerExternaIntBanklApi.updateIntBankStandardCreditCard(bankBasic);
			}
		}
	}
	
	
	
	/**
	 * 征信退回
	 * 
	 * @return
	 * @throws MarvelsBusinessException
	 */
	public Integer returnedOperating(String loanId) throws MarvelsBusinessException {
		// String loan = request("loanId").stringValue();
		LoanApplyCaseDTO dto = new LoanApplyCaseDTO();
		dto.setLoanId(loanId);
		dto.setCaseStatus(LoanStateConstant.JR0);
		dto.setUpdatedBy(getUserCode());
		dto.setLoanId(loanId);
		//dto.setStatus("-3");
		Integer count;
		try {
		 count = loanApplyCaseMapper.updateByPrimaryKeySelective(dto);
		// SQL sqlup = SQL.begin().sql("update ac_loan_credit_investigation set
		// status='1' where loan_id='"+loan+"'").end();
		// int count = JdbcTemplateEx.getInstance().update(sqlup);
		// this.setTypeTwo("1");
		// SQL sql =SQL.begin().sql(" SELECT count(id) FROM
		// ac_loan_credit_investigation A WHERE A.`status`='3' AND (operator = ?
		// OR operator IS NULL OR operator = '') ").end();
		// totalCount = JdbcTemplateEx.getInstance().queryForInt(sql.getText(),
		// getUserId());//总记录数
		// int start=(page-1)*pageSize;
		// int end=pageSize;
		// String hql = "SELECT new
		// net.jrx.finance.credit.entity.AcLoanCreditInvestigation(ac.id,ac.loanId,ac.productName,aa.custIdCard,ac.custName,ac.processAppTime,ac.userName,ac.operatedDate,ac.operator,ac.commitTime,ac.uploadTime,ac.status,ac.chargebackResBig,ac.chargebackResTiny,ac.chargebackDes,ac.status2,ac.handlingName)
		// FROM AcLoanCreditInvestigation ac , AcLoanApplyInfo aa WHERE
		// ac.loanId = aa.loanId and ac.status='3' AND (ac.operator = ? OR
		// ac.operator IS NULL OR ac.operator = '') ORDER BY ac.commitTime
		// desc";
		// this.setAcloancreditinvestigationList(genericDao.findPart(hql, start,
		// end,getUserId()));
		} catch (Exception e) {
			throw new MarvelsBusinessException(loanId + ">>>>>>>> 征信录入第三阶段否决出错：",e);
		}
		return count;
	}

	/**
	 * 征信否决
	 * 
	 * @return
	 * @throws MarvelsBusinessException
	 */
	public Integer rejectOperating(String loanId) throws MarvelsBusinessException {
		int count;
		try {
			// String loan = request("loanId").stringValue();
			LoanApplyCaseDTO dto = new LoanApplyCaseDTO();
			dto.setLoanId(loanId);
			dto.setCaseStatus(LoanStateConstant.JR0);
			dto.setUpdatedBy(getUserCode());
			dto.setLoanId(loanId);
			//dto.setStatus("-3");
			count = loanApplyCaseMapper.updateByPrimaryKeySelective(dto);
			//count = taskProcessMapper.updateLoanStatus(loanDto);
		} catch (Exception e) {
			throw new MarvelsBusinessException(loanId + ">>>>>>>> 征信录入第三阶段否决出错：", e);
		}
		return count;
	}
	
	public String getUserCode(){
		 UserDTO user = null;
		try {
			user = CacheUtils.getCacheUser();
		} catch (MarvelsBusinessException e) {
			e.printStackTrace();
			MarvelsLogUtil.getInstance().error("获取用户信息失败！");
		}
		 String userCode =user.getUserCode() == null?MarvelsConstant.SYSTEM_USER : user.getUserCode();
		 return userCode;
	}

	public void saveAllForm(IntBankTotalDTO intBankTotalDto) throws MarvelsBusinessException {
		try {
			if(intBankTotalDto.getIntBankBasicDto()!=null){//基本信息保存
				this.saveIntBankBasicList(intBankTotalDto.getIntBankBasicDto());
			}
			if(intBankTotalDto.getAddIntBankCreditPaymentRecordsDto()!=null){//贷款 增加
				this.updateLoanRecord(intBankTotalDto.getAddIntBankCreditPaymentRecordsDto());
			}
			if(intBankTotalDto.getUpdateIntBankCreditPaymentRecordsDto()!=null){//贷款修改
				this.updateRecord(intBankTotalDto.getUpdateIntBankCreditPaymentRecordsDto());
			}
			if(intBankTotalDto.getIntBankBanruDetailDto()!=null){//查询记录
				this.addBanruDetail(intBankTotalDto.getIntBankBanruDetailDto());
			}
			if(intBankTotalDto.getIntBankCredittipsDto()!=null){//信用提示
				this.updateCredittips(intBankTotalDto.getIntBankCredittipsDto());
			}
			if(intBankTotalDto.getIntBankDwellmessageDto()!=null){//居住信息
				this.saveIntBankDwellmessage(intBankTotalDto.getIntBankDwellmessageDto());
			}
			if(intBankTotalDto.getIntBankEndowmentInsuranceDto()!=null){//养老保险金缴存记录
				this.updateDetail(intBankTotalDto.getIntBankEndowmentInsuranceDto());
			}
			if(intBankTotalDto.getIntBankGuaranteeCountDto()!=null){//对外担保信息汇总
				this.updateGuaranteeCount(intBankTotalDto.getIntBankGuaranteeCountDto());
			}
			if(intBankTotalDto.getIntBankNotCredittipsDto()!=null){//授信及负债信息概要 未结清贷款信息汇总
				this.updateIntBankNotCredittips(intBankTotalDto.getIntBankNotCredittipsDto());
			}
			if(intBankTotalDto.getIntBankNotPinshouseholdsDto()!=null){//未销户贷记卡信息汇总
				this.updateIntBankNotPinshouseholds(intBankTotalDto.getIntBankNotPinshouseholdsDto());
			}
			if(intBankTotalDto.getIntBankNotQuasiCreditDto()!=null){//未销户准贷记卡信息汇总
				this.updateQuasiCredit(intBankTotalDto.getIntBankNotQuasiCreditDto());
			}
			if(intBankTotalDto.getIntBankPositionmesageDto()!=null){//职业信息
				this.addProfession(intBankTotalDto.getIntBankPositionmesageDto());
			}
			if(intBankTotalDto.getIntBankQueryDetailDto()!=null){//个人查询记录
				this.addQueryDetail(intBankTotalDto.getIntBankQueryDetailDto());
			}
			if(intBankTotalDto.getIntBankQuerySummaryDto()!=null){//查询记录汇总
				this.updateQueryLog(intBankTotalDto.getIntBankQuerySummaryDto());
			}
			if(!CollectionUtils.isEmpty(intBankTotalDto.getAddIntBankCreditCardDtoList())){//贷记卡 增加列表
				List<IntBankCreditCardDTO> list = intBankTotalDto.getAddIntBankCreditCardDtoList();
				for (IntBankCreditCardDTO addDto : list) {
//					this.addCredit(addDto);
				}
			}
			if(!CollectionUtils.isEmpty(intBankTotalDto.getUpdateIntBankCreditCardDtoList())){//贷记卡修改列表
				List<IntBankCreditCardDTO> list = intBankTotalDto.getUpdateIntBankCreditCardDtoList();
				for (IntBankCreditCardDTO updateDto : list) {
					this.updateCredit(updateDto);
				}
			}
			if(!CollectionUtils.isEmpty(intBankTotalDto.getAddIntBankStandardCreditCards())){//准贷记卡增加列表
				List<IntBankStandardCreditCardDTO> list = intBankTotalDto.getAddIntBankStandardCreditCards();
				for (IntBankStandardCreditCardDTO addDto : list) {
					this.addStandardCredit(addDto);
				}
			}
			if(!CollectionUtils.isEmpty(intBankTotalDto.getUpdateIntBankStandardCreditCards())){//准贷记卡修改列表
				List<IntBankStandardCreditCardDTO> list = intBankTotalDto.getUpdateIntBankStandardCreditCards();
				for (IntBankStandardCreditCardDTO updateDto : list) {
					this.updateStandardCredit(updateDto);
				}
			}
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("征信录入第三阶段一键保存异常",e);
			throw new MarvelsBusinessException(">>>>>>>> 征信录入第三阶段一键保存异常：", e);
		}
	}

	@Override
	public List<IntBankCreditPaymentRecordsDTO> saveIntBankCreditPaymentRecord(
			IntBankCreditPaymentRecordsDTO intBankCreditPaymentRecord) throws MarvelsBusinessException {
		if(intBankCreditPaymentRecord == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		List<IntBankCreditPaymentRecordsDTO> data = null;
		if (intBankCreditPaymentRecord.getId() == null || intBankCreditPaymentRecord.getId() < 1) {
			intBankCreditPaymentRecord.setEstablishDate(new Date());
			data = consumerExternaIntBanklApi.saveMarvelsIntBankCreditPaymentRecord(intBankCreditPaymentRecord);
		} else {
			intBankCreditPaymentRecord.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankCreditPaymentRecords(intBankCreditPaymentRecord);
		}
		return data;
	}
	
	//更新贷记卡信息
	@Override
	public List<IntBankCreditCardDTO> saveCreditCard(IntBankCreditCardDTO intBankCreditCard)  throws MarvelsBusinessException {
		if(intBankCreditCard == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		List<IntBankCreditCardDTO> data = null;
		if (intBankCreditCard.getId() == null || intBankCreditCard.getId() < 1) {
			intBankCreditCard.setEstablishDate(new Date());
			consumerExternaIntBanklApi.saveIntBankCreditCard(intBankCreditCard);
			Map<String, String> map = new HashMap<String, String>();
			map.put("serialNum", intBankCreditCard.getLoanId());
			Map<String, Object> dataMap = consumerExternaIntBanklApi.intBankCreditCard(map);
			Object intBankCreditCardNXh=dataMap.get("intBankCreditCardNXh");
			if(intBankCreditCardNXh != null){
				data=(List<IntBankCreditCardDTO>)intBankCreditCardNXh;
			}
		} else {
			intBankCreditCard.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankCreditCard(intBankCreditCard);
		}
		return data;
	}

	@Override
	public List<IntBankStandardCreditCardDTO> saveStandardCreditCard(
			IntBankStandardCreditCardDTO intBankStandardCreditCard) throws MarvelsBusinessException {
		if(intBankStandardCreditCard == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		List<IntBankStandardCreditCardDTO> data = null;
		if (intBankStandardCreditCard.getId() == null || intBankStandardCreditCard.getId() < 1) {
			intBankStandardCreditCard.setEstablishDate(new Date());
			consumerExternaIntBanklApi.saveIntBankStandardCreditCard(intBankStandardCreditCard);
			Map<String, String> map = new HashMap<String, String>();
			map.put("serialNum", intBankStandardCreditCard.getLoanId());
			data = consumerExternaIntBanklApi.intBankStandardCreditCard(map);
		} else {
			intBankStandardCreditCard.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankStandardCreditCard(intBankStandardCreditCard);
		}
		return data;
	}

	@Override
	public IntBankEndowmentInsuranceDTO saveIntBankEndowmentInsurance(
			IntBankEndowmentInsuranceDTO intBankEndowmentInsurance) throws MarvelsBusinessException {
		if(intBankEndowmentInsurance == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		List<IntBankEndowmentInsuranceDTO> data = null;
		if (intBankEndowmentInsurance.getId() == null || intBankEndowmentInsurance.getId() < 1) {
			intBankEndowmentInsurance.setEstablishDate(new Date());
			consumerExternaIntBanklApi.saveIntBankEndowmentInsurance(intBankEndowmentInsurance);
		} else {
			intBankEndowmentInsurance.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankEndowmentInsurance(intBankEndowmentInsurance);
		}
		Map<String, String> map=new HashMap<>();
		map.put("serialNum", intBankEndowmentInsurance.getLoanId());
		data = consumerExternaIntBanklApi.intBankEndowmentInsurance(map);
		if(!CollectionUtils.isEmpty(data)){
			return data.get(0);
		}
		return null;
	}

	@Override
	public IntBankQuerySummaryDTO saveIntBankQuerySummary(IntBankQuerySummaryDTO intBankQuerySummary)
			throws MarvelsBusinessException {
		if(intBankQuerySummary == null){
			throw new MarvelsBusinessException("入参不能为空");
		}
		List<IntBankQuerySummaryDTO> data = null;
		if (intBankQuerySummary.getId() == null || intBankQuerySummary.getId() < 1) {
			intBankQuerySummary.setEstablishDate(new Date());
			consumerExternaIntBanklApi.saveIntBankQuerySummary(intBankQuerySummary);
		} else {
			intBankQuerySummary.setAmendDate(new Date());
			consumerExternaIntBanklApi.updateIntBankQuerySummary(intBankQuerySummary);
		}
		Map<String, String> map=new HashMap<>();
		map.put("serialNum", intBankQuerySummary.getLoanId());
		data = consumerExternaIntBanklApi.intBankQuerySummary(map);
		if(CollectionUtils.isNotEmpty(data)){
			return data.get(0);
		}
		return null;
	}

	@Override
	public List<IntBankQueryDetailDTO> saveIntBankQueryDetail(IntBankQueryDetailDTO intBankQueryDetail)
			throws MarvelsBusinessException {
		if(intBankQueryDetail == null || StringUtils.isEmpty(intBankQueryDetail.getLoanId())){
			throw new MarvelsBusinessException("入参不能为空");
		}
		if(intBankQueryDetail.getQueryTheDate()==null || StringUtils.isEmpty(intBankQueryDetail.getQueryOperators())){
			throw new MarvelsBusinessException("查询日期和查询操作员不能为空");
		}
		intBankQueryDetail.setEstablishDate(new Date());
		List<IntBankQueryDetailDTO> data = null;
		consumerExternaIntBanklApi.saveIntBankQueryDetail(intBankQueryDetail);
		Map<String, String> map=new HashMap<>();
		map.put("serialNum", intBankQueryDetail.getLoanId());
		Map<String,Object> dataMap = consumerExternaIntBanklApi.addQueryDetailPositionData(map);
		Object intBankQueryDetailObj = dataMap.get("intBankQueryDetail");
		if(intBankQueryDetailObj!=null){
			data = (List<IntBankQueryDetailDTO>)intBankQueryDetailObj;
		}
		return data;
	}

	@Override
	public List<IntBankBanruDetailDTO> saveIntBankBanruDetail(IntBankBanruDetailDTO intBankBanruDetail)
			throws MarvelsBusinessException {
		if(intBankBanruDetail == null || StringUtils.isEmpty(intBankBanruDetail.getLoanId())){
			throw new MarvelsBusinessException("入参不能为空");
		}
		if(intBankBanruDetail.getQueryTheDate()==null || StringUtils.isEmpty(intBankBanruDetail.getQueryOperators())){
			throw new MarvelsBusinessException("查询日期和查询操作员不能为空");
		}
		intBankBanruDetail.setCreatedBy(CacheUtils.getCacheUser().getUserCode());
		intBankBanruDetail.setCreatedDate(new Date());
		intBankBanruDetail.setUpdatedBy(CacheUtils.getCacheUser().getUserCode());
		intBankBanruDetail.setUpdatedDate(new Date());
		List<IntBankBanruDetailDTO> data = null;
		consumerExternaIntBanklApi.saveIntBankBanruDetail(intBankBanruDetail);
		Map<String, String> map=new HashMap<>();
		map.put("serialNum", intBankBanruDetail.getLoanId());
		data = consumerExternaIntBanklApi.intBankBanruDetail(map);
		return data;
	}
	
	@Override
	public MarvelsResultDTO submitCreditReport(String loanId) throws MarvelsBusinessException {
		Map<String, String> map = new HashMap<>();
		map.put("loanId", loanId);
		List<IntBankBasicDTO>  intBankBasicList = consumerExternaIntBanklApi.intBankBasic(map);
		if(CollectionUtils.isEmpty(intBankBasicList)){
			throw new MarvelsBusinessException("个人基本信息不能为空");
		}
		IntBankBasicDTO intBankBasic = intBankBasicList.get(0);
		McamLoanApplyInfoDTO acLoanApplyInfo = taskProcessService.getLoanInfo(loanId);
		if(!StringUtils.equals(acLoanApplyInfo.getCustCardNo(), intBankBasic.getApplicantIdNbr())){
			throw new MarvelsBusinessException("报告身份证和进件身份证不匹配。征信报告身份证："+intBankBasic.getApplicantIdNbr()+";进件身份证："+acLoanApplyInfo.getCustCardNo());
		}
		//流程流转到征信前置
		CompleteProcessDTO proDto = new CompleteProcessDTO();
		ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
		processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);
		processApproveDto.setLoanId(loanId);
		proDto.setProcessApproveDto(processApproveDto);
		proDto.setLoanId(loanId);
		processService.completeLoanApplyCase(proDto);
		
		//调用核心第三方 更新数据
		MarvelsResultDTO apiResult = consumerExternaIntBanklApi.saveMarvelsCreditReport(loanId);
		if(!StringUtils.equals(apiResult.getCode(), MarvelsErrorCode.SUCCESS.getValue())){
			throw new MarvelsBusinessException("调用dubbo更新数据异常");
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null);
	}

	@Override
	public void returnCreditReport(String loanId) throws MarvelsBusinessException {
		if(StringUtils.isEmpty(loanId)){
			throw new MarvelsBusinessException("订单号不能为空");
		}
		CompleteProcessDTO proDto = new CompleteProcessDTO();
		ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
		processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_RETURN);
		processApproveDto.setLoanId(loanId);
		proDto.setProcessApproveDto(processApproveDto);
		proDto.setLoanId(loanId);
		processService.completeLoanApplyCase(proDto);
	}
}
